package com.jijia.webbase.config.security;

import com.jijia.webbase.domain.User;
import com.jijia.webbase.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

// 3. 自定义UserDetailsService
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
//        遍历user的所有属性 如果为空 则设置 空
        user.setUserName(user.getUserName()==null?"":user.getUserName());
        user.setUserPassword(user.getUserPassword()==null?"":user.getUserPassword());
        user.setAge(user.getAge()==null?0:user.getAge());
        user.setJob(user.getJob()==null?"":user.getJob());
        user.setRoles(user.getRoles()==null?"":user.getRoles());
        return new LoginUser(user);
    }
}